package com.kay.springboot_es_api.controller;

import bboss.org.apache.velocity.runtime.directive.Foreach;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.kay.springboot_es_api.domain.*;
import com.kay.springboot_es_api.utils.MyResultUtil;
import org.frameworkset.elasticsearch.ElasticSearchHelper;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.frameworkset.elasticsearch.client.ResultUtil;
import org.frameworkset.elasticsearch.entity.ESDatas;
import org.frameworkset.elasticsearch.serial.ESInnerHitSerialThreadLocal;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Kay on 2018/11/4
 */
@RestController
public class TestController {
    @GetMapping("/hello")
    public String Hello(){
        return "hello world";
    }

    @PostMapping("/user")
    public Object GetTestUser(@RequestBody TestUser user){
        return user;
    }

    @PostMapping("/Getall")
    public ClientInfo GetESAll(@RequestBody Filters filters){
//        System.out.println(filters.toString());
        // 参数设置
        Map<String,Object> params = new HashMap<String, Object>();
        ClientInfo clientInfo = new ClientInfo();
        List<Basic> basicslst = new ArrayList<Basic>();
        List<Diagnosis> diagnoseslst = new ArrayList<Diagnosis>();
        List<Medical> medicalslst = new ArrayList<Medical>();
        List<Exam> examslst = new ArrayList<Exam>();

        ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/queryDsl.xml");
//        ESDatas<Basic> basics = clientUtil.searchList("client_info/basic/_search", "testqueryall",para, Basic.class);
        try {
            ESInnerHitSerialThreadLocal.setESInnerTypeReferences("diagnosis",Diagnosis.class);//指定inner查询结果对于
            ESInnerHitSerialThreadLocal.setESInnerTypeReferences("medical", Medical.class);
            ESInnerHitSerialThreadLocal.setESInnerTypeReferences("exam", Exam.class);
            ESDatas<Basic> basics = clientUtil.searchList("client_info/basic/_search", "testqueryall", Basic.class);

            System.out.println("====================  totalsize: " + basics.getTotalSize());

            List<Basic> datas = basics.getDatas();
            for ( int i =0  ; i< datas.size(); i++){
                Basic basic = datas.get(i);
                basicslst.add(new Basic(basic.getParty_id(),
                                        basic.getSex(),
                                        basic.getMari_sts(),
                                        basic.getEthnic(),
                                        basic.getProf(),
                                        basic.getProvince(),
                                        basic.getCity(),
                                        basic.getClient_type(),
                                        basic.getClient_name(),
                                        basic.getAge(),
                                        basic.getId_type(),
                                        basic.getIdno(),
                                        basic.getEducation(),
                                        basic.getCreated_date(),
                                        basic.getBirth_date(),
                                        basic.getLast_modified_date(),
                                        basic.getEtl_date())) ;
                Integer age = basic.getAge();
//                System.out.println(basic.toString());
//                System.out.println("===================>>>>>>>>>>>  diagnosis");
                List<Diagnosis> diagnoses = ResultUtil.getInnerHits(basic.getInnerHits(), "diagnosis");

                if ( diagnoses == null)
                {
//                    System.out.println("================ diagnosis size : 0 ");
//                    diagnoses.toString();//
                }else
                {
//                    System.out.println("================ diagnosis size :" + diagnoses.size());
//                    System.out.println(diagnoses.toString());
                    for(Diagnosis d  : diagnoses){
                        d.setAge(age);
                    }
                    diagnoseslst.addAll(diagnoses);
                }

//                System.out.println("===================>>>>>>>>>>>  medical");
                List<Medical> medicals = ResultUtil.getInnerHits(basic.getInnerHits(),"medical");
                if (  medicals == null)
                {
//                    System.out.println("================ medicals size : 0 ");

                }else
                {
//                    System.out.println("================ medicals size :" + medicals.size());
//                    System.out.println(medicals.toString());
                    for(Medical d  : medicals){
                        d.setAge(age);
                    }
                    medicalslst.addAll(medicals);
                }
//                System.out.println("===================>>>>>>>>>>>  exam");
                List<Exam> exams = ResultUtil.getInnerHits(basic.getInnerHits(),"exam");
                if (  exams == null)
                {
//                    System.out.println("================ exams size : 0 ");

                }else
                {
//                    System.out.println("================ exams size :" + exams.size());
//                    System.out.println(exams.toString());
                    for(Exam d  : exams){
                        d.setAge(age);
                    }
                    examslst.addAll(exams);
                }
            }
            if(basicslst == null){
                System.out.println("no clientInfo ");
            }else
            {
                clientInfo.setBasics(basicslst);
                clientInfo.setDiagnoses(diagnoseslst);
                clientInfo.setMedicals(medicalslst);
                clientInfo.setExams(examslst);
                clientInfo.setTotalSize(basics.getTotalSize());
            }
//            return basics;
            return clientInfo;
        }finally{
            ESInnerHitSerialThreadLocal.clean();//清空inner查询结果对于雇员类型
        }
    }
}
